package easy;

public class Solution_1859 {

    public String sortSentence(String s) {
        s += " ";
        int[][] array = new int[10][];
        char[] chars = s.toCharArray();
        for (int i = 0, count = 0; i < chars.length; i++) {
            char c = chars[i];
            if (c == ' ') {
                int index = chars[i - 1] - '0';
                array[index] = new int[]{i - count, count-1};
                count = 0;
            } else {
                count++;
            }
        }
        StringBuffer buffer = new StringBuffer();
        for (int i = 0, count = 0; i < array.length; i++) {
            if (array[i] != null) {
                if (count > 0) {
                    buffer.append(' ');
                }
                buffer.append(chars, array[i][0], array[i][1]);
                count = 1;
            }
        }
        return buffer.toString();
    }
}
